package com.xy.biz.ts.domain.converter;

import java.util.Collection;
import java.util.List;

import com.xy.biz.base.domain.resp.ListDictItemResponse;
import com.xy.biz.ts.domain.bo.TrainDocumentRelBO;
import com.xy.biz.ts.domain.query.TrainUserQuery;
import com.xy.biz.ts.domain.req.*;
import com.xy.biz.ts.domain.resp.*;
import com.xy.lang.domain.KeyValue;
import com.xy.tool.util.BeanUtilsEx;
import com.xy.biz.ts.domain.bo.TrainBO;
import com.xy.biz.ts.domain.po.TrainPO;
import com.xy.biz.ts.domain.query.TrainQuery;

/**
 *
 *
 * @author wwj
 * @date 2021-4-1
 */
public class TrainConverter {

    public static TrainPO toPO(TrainBO source) {
        return BeanUtilsEx.copyProperties(source, () -> new TrainPO(), true);
    }

    public static List<TrainPO> toPO(Collection<TrainBO> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new TrainPO(), true);
    }

    public static TrainBO fromPO(TrainPO source) {
        return BeanUtilsEx.copyProperties(source, () -> new TrainBO());
    }

    public static List<TrainBO> fromPO(Collection<TrainPO> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new TrainBO());
    }

    public static TrainBO toBO(SaveTrainRequest source) {
        return BeanUtilsEx.copyProperties(source, () -> new TrainBO(), true);
    }

    public static TrainBO toBO(UpdateTrainRequest source) {
        return BeanUtilsEx.copyProperties(source, () -> new TrainBO(), true);
    }

    public static List<TrainBO> toBO(Collection<SaveTrainRequest> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new TrainBO(), true);
    }

    public static List<KeyValue> toKeyValueList(Collection<ListDictItemResponse> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new KeyValue(), (src, tar) -> {
            tar.setKey(src.getDictCode());
            tar.setValue(src.getDictValue());
        });
    }

    public static GetTrainResponse toResponse(TrainBO source) {
        return BeanUtilsEx.copyProperties(source, () -> new GetTrainResponse(),
                (src, tar) -> {
                    tar.setTrainUserResponses(BeanUtilsEx.copyPropertiesForNewList(src.getTrainUserBOS(), () -> new TrainUserResponse()));
                    tar.setTrainUserGroupResponses(BeanUtilsEx.copyPropertiesForNewList(src.getTrainUserGroupBOS(), () -> new TrainUserGroupResponse()));
                    tar.setTrainDocumentRelResponses(BeanUtilsEx.copyPropertiesForNewList(src.getTrainDocumentRelBOS(), () -> new GetTrainDocumentRelResponse()));
                    tar.setTrainFlowHandleList(TrainFlowHandleConverter.toResponse(src.getTrainFlowHandleList()));
                });
    }

//	public static List<GetTrainResponse> toResponse(Collection<TrainBO> source) {
//		return BeanUtilsEx.copyPropertiesForNewList(source, () -> new GetTrainResponse());
//	}

    public static List<GetTrainResponse> toResponse(Collection<TrainBO> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new GetTrainResponse(),
                (src, tar) -> {
                    tar.setTrainUserResponses(BeanUtilsEx.copyPropertiesForNewList(src.getTrainUserBOS(), () -> new TrainUserResponse()));
                    tar.setTrainUserGroupResponses(BeanUtilsEx.copyPropertiesForNewList(src.getTrainUserGroupBOS(), () -> new TrainUserGroupResponse(),
                            (s, t) -> t.setTrainUserResponses(BeanUtilsEx.copyPropertiesForNewList(s.getTrainUserBOS(), () -> new TrainUserResponse()))));
                    tar.setTrainDocumentRelResponses(BeanUtilsEx.copyPropertiesForNewList(src.getTrainDocumentRelBOS(), () -> new GetTrainDocumentRelResponse()));
                    tar.setPracticeRecordList(BeanUtilsEx.copyPropertiesForNewList(src.getPracticeRecordList(), () -> new GetPracticeRecordResponse()));
                }

                //tar.setTrainDocumentRelResponses(BeanUtilsEx.copyPropertiesForNewList(src.getTrainDocumentRelBOS(), () -> new GetTrainDocumentRelResponse()))
        );
    }


    public static TrainQuery toQuery(ListTrainRequest source) {
        if (null == source) {
            return null;
        }
        TrainQuery target = new TrainQuery(source.getPageIndex(), source.getPageSize());
        BeanUtilsEx.copyProperties(source, target);
        return target;
    }

    public static TrainUserQuery toQuery(ListRecordByTrainRequest source) {
        if (null == source) {
            return null;
        }
        TrainUserQuery target = new TrainUserQuery(0, Integer.MAX_VALUE);
        BeanUtilsEx.copyProperties(source, target);
        return target;
    }

    public static TrainUserQuery toQuery(GetUserByTrainRequest source) {
        if (null == source) {
            return null;
        }
        TrainUserQuery target = new TrainUserQuery(source.getPageIndex(), source.getPageSize());
        BeanUtilsEx.copyProperties(source, target);
        return target;
    }

}
